<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <link rel="icon" href="pic/favicon.ico" type="image/x-icon">
    <script src="axios.min.js"></script>
    <script src="vue.global.js"></script>
    <link href="https://fonts.googleapis.com/css2?family=Pacifico&display=swap" rel="stylesheet">
    <style>
        /* 全局样式 */
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Arial', sans-serif;
            background-image: url("./pic/绿色.png");
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            color: #fff;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            position: relative;
        }

        /* 标题样式 */
        h3 {
            font-size: 4em;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5), -2px -2px 4px rgba(255, 255, 255, 0.5);
            margin-bottom: 20px;
            z-index: 1;
            text-align: center;
        }

        /* 灰色半透明盒子 */
        .box {
            background-color: rgba(128, 128, 128, 0.7);
            padding: 40px;
            border-radius: 10px;
            text-align: center;
            position: relative;
            width: 400px;
            z-index: 1;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }

        /* 登录功能样式 */
        .login {
            margin-bottom: 20px;
        }

        .login label {
            display: inline-block;
            width: 80px;
            text-align: right;
            margin-right: 10px;
            font-size: 1.2em;
        }

        .login input {
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: calc(100% - 100px);
            font-size: 1em;
            box-sizing: border-box;
        }

        .login button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            font-size: 1em;
            cursor: pointer;
            transition: background-color 0.3s ease;
            width: 100%;
            max-width: 120px;
            margin: 10px auto;
            display: block;
        }

        .login button:hover {
            background-color: #0056b3;
        }

        /* 注册功能样式 */
        .register {
            position: absolute;
            bottom: 10px;
            right: 10px;
            font-size: 0.8em;
        }

        .register a {
            text-decoration: none;
            color: #fff;
            transition: color 0.3s ease;
        }

        .register a:hover {
            color: #007BFF;
        }
    </style>
</head>
<body>
<div id="app">
    <h3>用户登录</h3>
    <div class="box">
        <div class="login">
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username" v-model="username"><br>
            <label for="password">密&nbsp;&nbsp;&nbsp;&nbsp;码：</label>
            <input type="password" id="password" name="password" v-model="password"><br>
            <button @click="login">登录</button>
        </div>
        <div class="register">
            <a href="registration.html">注册</a>
        </div>
    </div>
</div>
<script>
    const app = {
        data() {
            return {
                username: '',
                password: ''
            };
        },
        methods: {
            async login() {
                console.log("用户开始登录！");


                const response = await axios.post('/login', {
                    username: this.username,
                    password: this.password
                });

                if (response.data.status === 'ok') {
                    //根据用户名查找用户id


                    // 存储用户信息到 localStorage
                    localStorage.setItem('user', JSON.stringify({username: this.username,id:response.data.id}));
                    console.log(this.user_id + response.data.id)

                    alert("登录成功！");
                    window.location.href = "index.html"; // 跳转回主页面
                } else {
                    alert("登录失败：" + response.data);
                    console.log(response)
                }

            }
        },
        mounted() {
            console.log("Vue启动成功！");
        }
    };

    Vue.createApp(app).mount("#app");
</script>
</body>
</html>